package com.spotbook.ui;

import android.app.LoaderManager.LoaderCallbacks;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.spotbook.R;
import com.spotbook.providers.Contract;

public class SpotMapActivity extends MapActivity implements
		LoaderCallbacks<Cursor> {
	ListView list;
	EditText search;
	ImageButton button;
	MapView map;

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.map);
		this.map = (MapView) findViewById(R.id.map);
		this.search = (EditText) findViewById(R.id.search);
		this.button = (ImageButton) findViewById(android.R.id.button1);
		this.list = (ListView) findViewById(android.R.id.list);
		getLoaderManager().initLoader(0, null, this);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle extra) {
		return new CursorLoader(this, Contract.CONTENT_URI, null, null, null,
				null);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		if (data.moveToFirst()) {
			Drawable drawable = this.getResources().getDrawable(
					R.drawable.ic_launcher);
			final HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
					drawable, this);
			double maxLat = -90;
			double maxLng = 90;
			double minLat = 180;
			double minLng = -180;
			GeoPoint singleCenter = null;
			do {
				ContentValues values = new ContentValues();
				DatabaseUtils.cursorRowToContentValues(data, values);
				String name = values.getAsString(Contract._NAME);
				double longitude = values.getAsDouble(Contract._LONGITUDE);
				double latitude = values.getAsDouble(Contract._LATITUDE);
				maxLat = Math.max(maxLat, latitude);
				minLat = Math.min(minLat, latitude);
				maxLng = Math.max(maxLng, longitude);
				minLng = Math.min(minLng, longitude);
				GeoPoint overlayPoint;
				if (data.getCount() > 1) {
					overlayPoint = new GeoPoint((int) (latitude * 1e6),
							(int) (longitude * 1e6));
				} else {
					overlayPoint = singleCenter = new GeoPoint(
							(int) (latitude * 1e6), (int) (longitude * 1e6));
				}
				OverlayItem overlayitem = new OverlayItem(overlayPoint, name,
						null);
				itemizedoverlay.addOverlay(overlayitem);
			} while (data.moveToNext());

			final GeoPoint center;
			final Integer latSpan;
			final Integer lngSpan;
			if (singleCenter == null) {
				double avgLat = (maxLat + minLat) / 2;
				double avgLng = (maxLng + minLng) / 2;
				center = new GeoPoint((int) (avgLat * 1e6),
						(int) (avgLng * 1e6));
				latSpan = Math.abs((int) ((maxLat - minLat) * 1e6));
				lngSpan = Math.abs((int) ((maxLng - minLng) * 1e6));
			} else {
				center = singleCenter;
				latSpan = null;
				lngSpan = null;
			}
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					map.getOverlays().clear();
					if (itemizedoverlay != null) {
						map.getOverlays().add(itemizedoverlay);
					}
					if (center != null) {
						if (latSpan != null && lngSpan != null) {
							map.getController().zoomToSpan(latSpan, lngSpan);
						} else {
							map.getController().setZoom(16);
						}
						map.getController().setCenter(center);
					}
				}
			});
		} else {
			onLoaderReset(loader);
		}
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				map.getOverlays().clear();
			}
		});
	}
}
